const treeData = require("./treeData")

//基于栈结构深度优先遍历树,栈结构需要保证里面的元素是后进先出
function traverseTreeByDepthStack(node){
    if(!node){
        return
    }
    const stack = []
    stack.push(node)    //根元素入栈
    while(stack.length>0){
        const currentNode = stack.pop()
        console.log("node:",currentNode.value)
        if(currentNode.children.length > 0){
            for(let i = currentNode.children.length-1;i >= 0;i--){
                stack.push(currentNode.children[i])     //子元素从后往前遍历入栈
            }
        }
    }
   
}

function testTraverseTreeByDepthStack(){
    const tree = treeData.getTree()
    traverseTreeByDepthStack(tree)
}

testTraverseTreeByDepthStack()